package net.lcqf.tcp.game;

import cn.hutool.json.JSONUtil;
import io.netty.channel.Channel;
import net.lcqf.tcp.game.match.model.MatchPlayer;
import net.lcqf.tcp.server.MsgInfoPojo;

import java.util.List;

/**
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2021-2025 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2025/3/13 09:46
 * // +----------------------------------------------------------------------
 */
public final class MatchRoom {


    /**
     * 发送消息给指定房间的全部玩家
     * @param type 发送类型
     * @param data 消息
     * @param playerList 所有玩家
     */
    public static void broadcastToPlayer(Integer type, String data, List<MatchPlayer> playerList) {
        MsgInfoPojo.MsgInfo.Builder builder = MsgInfoPojo.MsgInfo.newBuilder().setRequestType(type);

        for (MatchPlayer player : playerList) {
            MsgInfoPojo.MsgInfo response = builder.setData(data).build();
            if(player.getChannel().isActive()) { player.getChannel().writeAndFlush(response); }
        }
    }

    /**
     * 发送消息给指定玩家
     * @param type 发送类型
     * @param data 消息
     * @param channel 玩家通信标识
     */
    public static void SingleToPlayer(Integer type, String data, Channel channel) {
        MsgInfoPojo.MsgInfo response = MsgInfoPojo.MsgInfo.newBuilder()
                .setRequestType(type)
                .setData(JSONUtil.toJsonStr(data))
                .build();

        if(channel.isActive()) { channel.writeAndFlush(response); }
    }
}
